home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / GAS_1_38.ARJ / INPTSCRB.C < prev    next >
C/C++ Source or Header  |  1991-01-04  |  9KB  |  351 lines

  1. /* input_scrub.c - layer between app and the rest of the world
  2.    Copyright (C) 1987 Free Software Foundation, Inc.
  3.  
  4. This file is part of GAS, the GNU Assembler.
  5.  
  6. GAS is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GAS is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GAS; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "as.h"
  21. #include "read.h"
  22. #include "input-file.h"
  23.  
  24. /*
  25.  * O/S independent module to supply buffers of sanitised source code
  26.  * to rest of assembler. We get raw input data of some length.
  27.  * Also looks after line numbers, for e.g. error messages.
  28.  * This module used to do the sanitising, but now a pre-processor program
  29.  * (app) does that job so this module is degenerate.
  30.  * Now input is pre-sanitised, so we only worry about finding the
  31.  * last partial line. A buffer of full lines is returned to caller.
  32.  * The last partial line begins the next buffer we build and return to caller.
  33.  * The buffer returned to caller is preceeded by BEFORE_STRING and followed
  34.  * by AFTER_STRING. The last character before AFTER_STRING is a newline.
  35.  */
  36.  
  37. /*
  38.  * We expect the following sanitation has already been done.
  39.  *
  40.  * No comments, reduce a comment to a space.
  41.  * Reduce a tab to a space unless it is 1st char of line.
  42.  * All multiple tabs and spaces collapsed into 1 char. Tab only
  43.  *   legal if 1st char of line.
  44.  * # line file statements converted to .line x;.file y; statements.
  45.  * Escaped newlines at end of line: remove them but add as many newlines
  46.  *   to end of statement as you removed in the middle, to synch line numbers.
  47.  */
  48.  
  49. #define BEFORE_STRING ("\n")
  50. #define AFTER_STRING ("\0")    /* bcopy of 0 chars might choke. */
  51. #define BEFORE_SIZE (1)
  52. #define AFTER_SIZE  (1)
  53.  
  54. static char *    buffer_start;    /* -> 1st char of full buffer area. */
  55. static char *    partial_where;    /* -> after last full line in buffer. */
  56. static int    partial_size;    /* >=0. Number of chars in partial line in buffer. */
  57. static char    save_source [AFTER_SIZE];
  58.                 /* Because we need AFTER_STRING just after last */
  59.                 /* full line, it clobbers 1st part of partial */
  60.                 /* line. So we preserve 1st part of partial */
  61.                 /* line here. */
  62. static int    buffer_length;    /* What is the largest size buffer that */
  63.                 /* input_file_give_next_buffer() could */
  64.                 /* return to us? */
  65.  
  66. static void as_1_char ();
  67.  
  68. /*
  69. We never have more than one source file open at once.
  70. We may, however, read more than 1 source file in an assembly.
  71. NULL means we have no file open right now.
  72. */
  73.  
  74.  
  75. /*
  76. We must track the physical file and line number for error messages.
  77. We also track a "logical" file and line number corresponding to (C?)
  78. compiler source line numbers.
  79. Whenever we open a file we must fill in physical_input_file. So if it is NULL
  80. we have not opened any files yet.
  81. */
  82.  
  83. static
  84. char *        physical_input_file,
  85.      *        logical_input_file;
  86.  
  87.  
  88.  
  89. typedef unsigned int line_numberT;    /* 1-origin line number in a source file. */
  90.                 /* A line ends in '\n' or eof. */
  91.  
  92. static
  93. line_numberT    physical_input_line,
  94.         logical_input_line;
  95.  
  96. void
  97. input_scrub_begin ()
  98. {
  99.   know( strlen(BEFORE_STRING) == BEFORE_SIZE );
  100.   know( strlen( AFTER_STRING) ==  AFTER_SIZE );
  101.  
  102.   input_file_begin ();
  103.  
  104.   buffer_length = input_file_buffer_size ();
  105.  
  106.   buffer_start = xmalloc ((long)(BEFORE_SIZE + buffer_length + buffer_length + AFTER_SIZE));
  107.   bcopy (BEFORE_STRING, buffer_start, (int)BEFORE_SIZE);
  108.  
  109.   /* Line number things. */
  110.   logical_input_line = 0;
  111.   logical_input_file = (char *)NULL;
  112.   physical_input_file = NULL;    /* No file read yet. */
  113.   do_scrub_begin();
  114. }
  115.  
  116. void
  117. input_scrub_end ()
  118. {
  119.   input_file_end ();
  120. }
  121.  
  122. char *                /* Return start of caller's part of buffer. */
  123. input_scrub_new_file (filename)
  124.      char *    filename;
  125. {
  126.   input_file_open (filename, !flagseen['f']);
  127.   physical_input_file = filename[0] ? filename : "{standard input}";
  128.   physical_input_line = 0;
  129.  
  130.   partial_size = 0;
  131.   return (buffer_start + BEFORE_SIZE);
  132. }
  133.  
  134. char *
  135. input_scrub_next_buffer (bufp)
  136. char **bufp;
  137. {
  138.   register char *    limit;    /* -> just after last char of buffer. */
  139.  
  140. #ifdef DONTDEF
  141.   if(preprocess) {
  142.     if(save_buffer) {
  143.       *bufp = save_buffer;
  144.       save_buffer = 0;
  145.     }
  146.     limit = input_file_give_next_buffer(buffer_start+BEFORE_SIZE);
  147.     if (!limit) {
  148.       partial_where = 0;
  149.       if(partial_size)
  150.         as_warn("Partial line at end of file ignored");
  151.       return partial_where;
  152.     }
  153.  
  154.     if(partial_size)
  155.       bcopy(save_source, partial_where,(int)AFTER_SIZE);
  156.     do_scrub(partial_where,partial_size,buffer_start+BEFORE_SIZE,limit-(buffer_start+BEFORE_SIZE),&out_string,&out_length);
  157.     limit=out_string + out_length;
  158.     for(p=limit;*--p!='\n';)
  159.       ;
  160.     p++;
  161.     if(p<=buffer_start+BEFORE_SIZE)
  162.       as_fatal("Source line too long.  Please change file '%s' and re-make the assembler.",__FILE__);
  163.  
  164.     partial_where = p;
  165.     partial_size = limit-p;
  166.     bcopy(partial_where, save_source,(int)AFTER_SIZE);
  167.     bcopy(AFTER_STRING, partial_where, (int)AFTER_SIZE);
  168.  
  169.     save_buffer = *bufp;
  170.     *bufp = out_string;
  171.  
  172.     return partial_where;
  173.   }
  174.  
  175.   /* We're not preprocessing.  Do the right thing */
  176. #endif
  177.   if (partial_size)
  178.     {
  179.       bcopy (partial_where, buffer_start + BEFORE_SIZE, (int)partial_size);
  180.       bcopy (save_source, buffer_start + BEFORE_SIZE, (int)AFTER_SIZE);
  181.     }
  182.   limit = input_file_give_next_buffer (buffer_start + BEFORE_SIZE + partial_size);
  183.   if (limit)
  184.     {
  185.       register char *    p;    /* Find last newline. */
  186.  
  187.       for (p = limit;   * -- p != '\n';   )
  188.     {
  189.     }
  190.       ++ p;
  191.       if (p <= buffer_start + BEFORE_SIZE)
  192.     {
  193.       as_fatal ("Source line too long. Please change file %s then rebuild assembler.", __FILE__);
  194.     }
  195.       partial_where = p;
  196.       partial_size = limit - p;
  197.       bcopy (partial_where, save_source,  (int)AFTER_SIZE);
  198.       bcopy (AFTER_STRING, partial_where, (int)AFTER_SIZE);
  199.     }
  200.   else
  201.     {
  202.       partial_where = 0;
  203.       if (partial_size > 0)
  204.     {
  205.       as_warn( "Partial line at end of file ignored" );
  206.     }
  207.     }
  208.   return (partial_where);
  209. }
  210.  
  211. /*
  212.  * The remaining part of this file deals with line numbers, error
  213.  * messages and so on.
  214.  */
  215.  
  216.  
  217. int
  218. seen_at_least_1_file ()        /* TRUE if we opened any file. */
  219. {
  220.   return (physical_input_file != NULL);
  221. }
  222.  
  223. void
  224. bump_line_counters ()
  225. {
  226.   ++ physical_input_line;
  227.   ++ logical_input_line;
  228. }
  229.  
  230. /*
  231.  *            new_logical_line()
  232.  *
  233.  * Tells us what the new logical line number and file are.
  234.  * If the line_number is <0, we don't change the current logical line number.
  235.  * If the fname is NULL, we don't change the current logical file name.
  236.  */
  237. void
  238. new_logical_line (fname, line_number)
  239.      char *    fname;        /* DON'T destroy it! We point to it! */
  240.      int    line_number;
  241. {
  242.   if ( fname )
  243.     {
  244.       logical_input_file = fname;
  245.     }
  246.   if ( line_number >= 0 )
  247.     {
  248.       logical_input_line = line_number;
  249.     }
  250. }
  251.  
  252. /*
  253.  *            a s _ w h e r e ( )
  254.  *
  255.  * Write a line to stderr locating where we are in reading
  256.  * input source files.
  257.  * As a sop to the debugger of AS, pretty-print the offending line.
  258.  */
  259. void
  260. as_where()
  261. {
  262.   char *p;
  263.   line_numberT line;
  264.  
  265.   if (physical_input_file)
  266.     {                /* we tried to read SOME source */
  267.       if (input_file_is_open())
  268.     {            /* we can still read lines from source */
  269. #ifdef DONTDEF
  270.       fprintf (stderr," @ physical line %ld., file \"%s\"",
  271.            (long) physical_input_line, physical_input_file);
  272.       fprintf (stderr," @ logical line %ld., file \"%s\"\n",
  273.            (long) logical_input_line, logical_input_file);
  274.       (void)putc(' ', stderr);
  275.       as_howmuch (stderr);
  276.       (void)putc('\n', stderr);
  277. #else
  278.         p = logical_input_file ? logical_input_file : physical_input_file;
  279.         line = logical_input_line ? logical_input_line : physical_input_line;
  280.         fprintf(stderr,"%s:%u:", p, line);
  281. #endif
  282.     }
  283.       else
  284.     {
  285. #ifdef DONTDEF
  286.       fprintf (stderr," After reading source.\n");
  287. #else
  288.     p = logical_input_file ? logical_input_file : physical_input_file;
  289.     line = logical_input_line ? logical_input_line : physical_input_line;
  290.     fprintf (stderr,"%s:unknown:", p);
  291. #endif
  292.     }
  293.     }
  294.   else
  295.     {
  296. #ifdef DONTDEF
  297.       fprintf (stderr," Before reading source.\n");
  298. #else
  299. #endif
  300.     }
  301. }
  302.  
  303.  
  304.  
  305.  
  306. /*
  307.  *            a s _ h o w m u c h ( )
  308.  *
  309.  * Output to given stream how much of line we have scanned so far.
  310.  * Assumes we have scanned up to and including input_line_pointer.
  311.  * No free '\n' at end of line.
  312.  */
  313. void
  314. as_howmuch (stream)
  315.      FILE * stream;        /* Opened for write please. */
  316. {
  317.   register    char *    p;    /* Scan input line. */
  318.   /* register    char    c; JF unused */
  319.  
  320.   for (p = input_line_pointer - 1;   * p != '\n';   --p)
  321.     {
  322.     }
  323.   ++ p;                /* p -> 1st char of line. */
  324.   for (;  p <= input_line_pointer;  p++)
  325.     {
  326.       /* Assume ASCII. EBCDIC & other micro-computer char sets ignored. */
  327.       /* c = *p & 0xFF; JF unused */
  328.       as_1_char (*p, stream);
  329.     }
  330. }
  331.  
  332. static void
  333. as_1_char (c,stream)
  334.      unsigned char c;
  335.      FILE *    stream;
  336. {
  337.   if ( c > 127 )
  338.     {
  339.       (void)putc( '%', stream);
  340.       c -= 128;
  341.     }
  342.   if ( c < 32 )
  343.     {
  344.       (void)putc( '^', stream);
  345.       c += '@';
  346.     }
  347.   (void)putc( c, stream);
  348. }
  349.  
  350. /* end: input_scrub.c */
  351.